package com.xiexin.web.controller.group;

import com.xiexin.common.annotation.Log;
import com.xiexin.common.core.controller.BaseController;
import com.xiexin.common.core.domain.AjaxResult;
import com.xiexin.common.core.page.TableDataInfo;
import com.xiexin.common.enums.BusinessType;
import com.xiexin.common.utils.poi.ExcelUtil;
import com.xiexin.group.domain.ItemAndGroup;
import com.xiexin.group.service.IItemAndGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品和组的中间Controller
 *
 * @author xiexin
 * @date 2022-03-21
 */
@RestController
@RequestMapping("/group/itemgroup")
public class ItemAndGroupController extends BaseController {
    @Autowired
    private IItemAndGroupService itemAndGroupService;

    /**
     * 查询产品和组的中间列表
     */
    @PreAuthorize("@ss.hasPermi('group:itemgroup:list')")
    @GetMapping("/list")
    public TableDataInfo list(ItemAndGroup itemAndGroup) {
        startPage();
        List<ItemAndGroup> list = itemAndGroupService.selectItemAndGroupList(itemAndGroup);
        return getDataTable(list);
    }

    /**
     * 导出产品和组的中间列表
     */
    @PreAuthorize("@ss.hasPermi('group:itemgroup:export')")
    @Log(title = "产品和组的中间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItemAndGroup itemAndGroup) {
        List<ItemAndGroup> list = itemAndGroupService.selectItemAndGroupList(itemAndGroup);
        ExcelUtil<ItemAndGroup> util = new ExcelUtil<ItemAndGroup>(ItemAndGroup.class);
        util.exportExcel(response, list, "产品和组的中间数据");
    }

    /**
     * 获取产品和组的中间详细信息
     */
  @PreAuthorize("@ss.hasPermi('group:itemgroup:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
      List<ItemAndGroup> itemAndGroups = itemAndGroupService.selectItemAndGroupById(id);



      return AjaxResult.success(itemAndGroups);
    }

//    @PreAuthorize("@ss.hasPermi('group:itemgroup:query')")
//    @GetMapping(value = "/{id}")
//    public Map getInfo(@PathVariable("id") Integer id) {
//      List<ItemAndGroup> itemAndGroups = itemAndGroupService.selectItemAndGroupById(id);
//        Map codeMap = new HashMap();
//        codeMap.put("code",200);
//        codeMap.put("msg","yes");
//        codeMap.put("data",itemAndGroups);
//
//
//      return codeMap;
//    }


    /**
     * 新增产品和组的中间
     */
    @PreAuthorize("@ss.hasPermi('group:itemgroup:add')")
    @Log(title = "产品和组的中间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItemAndGroup itemAndGroup) {
        return toAjax(itemAndGroupService.insertItemAndGroup(itemAndGroup));
    }

    /**
     * 修改产品和组的中间
     */
    @PreAuthorize("@ss.hasPermi('group:itemgroup:edit')")
    @Log(title = "产品和组的中间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItemAndGroup itemAndGroup) {
        return toAjax(itemAndGroupService.updateItemAndGroup(itemAndGroup));
    }

    /**
     * 删除产品和组的中间
     */
    @PreAuthorize("@ss.hasPermi('group:itemgroup:remove')")
    @Log(title = "产品和组的中间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(itemAndGroupService.deleteItemAndGroupByIds(ids));
    }
}
